//
//  BSHotViewController.m
//  BS
//
//  Created by lanou on 15/8/14.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#define kButtonTag 10000

#import "BSHotViewController.h"

#import "Header.h"

#import "BSHotTableVC.h"

#import "PlayerManger.h"

#import "BDKNotifyHUD.h"
@interface BSHotViewController ()<UIScrollViewDelegate>
{
    NSInteger _selectedButtonTag;
}
@property (strong, nonatomic)UIView *headerBar;

@property (strong, nonatomic)UIScrollView *scrollView;

@property (strong, nonatomic)NSMutableArray *parameterArray;//字典数组

@property (strong, nonatomic)NSMutableArray *headerArray;//头数组

@property (strong, nonatomic)BDKNotifyHUD *notify;
@end

@implementation BSHotViewController

- (NSMutableArray *)headerArray{

    if (_headerArray == nil) {
        _headerArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _headerArray;
}


- (NSMutableArray *)parameterArray{

    if (_parameterArray == nil) {
        _parameterArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _parameterArray;
}


- (NSString *)getFilePath{

    return [[NSBundle mainBundle] pathForResource:@"Hot" ofType:@"json"];
}



- (void)requestJsonData{

    NSString *filePath = [self getFilePath];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
    for (NSDictionary *dict in dataArray) {
        [self.headerArray addObject:[dict allKeys][0]];
        [self.parameterArray addObject:[dict allValues][0]];
    }
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self requestJsonData];
    [self createHeaderBar];
    [self createScrollView];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];
}


- (void)createHeaderBar{

    self.navigationController.navigationBarHidden = YES;//隐藏自带的navigationBar
    self.headerBar = [[UIView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
    CGFloat buttonWidth = SCREEN_WIDTH / [self.headerArray count] * 1.0;
    for (int i = 0; i < [self.headerArray count]; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        //默认刚开始第0个button被选中
        if (i == 0) {
            button.selected = YES;
            _selectedButtonTag = kButtonTag;
        }
        button.frame = CGRectMake(i * buttonWidth, 0, buttonWidth, 44);
        [button setTitle:self.headerArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(selectedHeaderBtn:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = kButtonTag + i;
        [self.headerBar addSubview:button];
    }
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 43, SCREEN_WIDTH, 1)];
    line.backgroundColor = [UIColor grayColor];
        [_headerBar addSubview:line];
    [self.view addSubview:self.headerBar];
}


- (void)selectedHeaderBtn:(UIButton *)btn{

    if (btn.tag != _selectedButtonTag) {
         int i = btn.tag - kButtonTag;
        btn.selected = YES;
        UIButton *previousBtn = (UIButton *)[self.headerBar viewWithTag:_selectedButtonTag];
        previousBtn.selected = NO;
        _selectedButtonTag = btn.tag;
        [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTH * i, 0) animated:NO];
        if ([PlayerManger defaultManger].mediaType == Vedio) {
            [[PlayerManger defaultManger] stop];
        }
        [self scrollViewDidEndDecelerating: self.scrollView];
    }
}


- (void)createScrollView{

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * [self.headerArray count], SCREEN_HEIGHT - 64);
        for (int i = 0; i < [self.headerArray count]; i++) {
          //懒加载 tableView
        BSHotTableVC *tableVC = [[BSHotTableVC alloc] initWithStyle:UITableViewStyleGrouped];
            tableVC.typeName = self.headerArray[i];
        if (i == 0) {
            tableVC.parameterDic = self.parameterArray[i];
            tableVC.view.frame = CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
            [self.scrollView addSubview:tableVC.view];
        }
      
        [self addChildViewController:tableVC];
    }
    [self.view addSubview:self.scrollView];
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    int i = scrollView.contentOffset.x / SCREEN_WIDTH;
    int currentTag = i + kButtonTag;
    if (currentTag != _selectedButtonTag) {
        UIButton *currentBtn = (UIButton *)[self.headerBar viewWithTag:currentTag];
        currentBtn.selected = YES;
        UIButton *previousBtn = (UIButton *)[self.headerBar viewWithTag:_selectedButtonTag];
        previousBtn.selected = NO;
        _selectedButtonTag = currentTag;
        if ([PlayerManger defaultManger].mediaType == Vedio) {
            [[PlayerManger defaultManger] stop];
        }
    }
    //懒加载 tableView
    if (i > 0) {
        BSHotTableVC *tableVC = self.childViewControllers[i];
        if ([tableVC.dataSource count] == 0) {
            tableVC.parameterDic = self.parameterArray[i];
            tableVC.view.frame = CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
            [self.scrollView addSubview:tableVC.view];
        }
    }
}

- (void)back{

 
}
@end
